/*
 * File:   BatchFilterDlg.cpp
 * Author: Morpheus
 *
 * Created on 2 juillet 2013, 15:14
 */

#include "BatchFilterDlg.h"

BatchFilterDlg::BatchFilterDlg(QWidget* parent)
 : QDialog(parent) {
    widget.setupUi(this);
    updateDisplay();
    adjustSize();
}

BatchFilterDlg::~BatchFilterDlg() {
}

BatchMergeCriteria BatchFilterDlg::getCritetia() const {
    return m_critetia;
}
void BatchFilterDlg::setCritetia(const BatchMergeCriteria& critetia) {
    this->m_critetia = critetia;
    updateDisplay();
}

void BatchFilterDlg::updateDisplay(){
    setUpdatesEnabled(false);
    widget.name->setText(m_critetia.name);
    if(m_critetia.createdDateTagAdv == BatchMergeCriteria::Beetween){
        if(widget.createdDateTag->count() >= 3){
            widget.createdDateTag->removeItem(2);
            widget.createdDateTag->removeItem(2);
        }
        if(m_critetia.createdDateTag >= BatchMergeCriteria::RelativeDate){
            m_critetia.createdDateTag = BatchMergeCriteria::TagDate(m_critetia.createdDateTag - 2);
        }
    }else if(widget.createdDateTag->count() == 2){
        widget.createdDateTag->addItem(tr("Relative Date"));
        widget.createdDateTag->addItem(tr("Relative Date + Heure"));
    }
    
    if(m_critetia.modifiedDateTagAdv == BatchMergeCriteria::Beetween){
        if(widget.modifiedDateTag->count() >= 3){
            widget.createdDateTag->removeItem(2);
            widget.createdDateTag->removeItem(2);
        }
        if(m_critetia.modifiedDateTag >= BatchMergeCriteria::RelativeDate){
            m_critetia.modifiedDateTag = BatchMergeCriteria::TagDate(m_critetia.modifiedDateTag - 2);
        }
    }else if(widget.modifiedDateTag->count() == 2){
        widget.modifiedDateTag->addItem(tr("Relative Date"));
        widget.modifiedDateTag->addItem(tr("Relative Date + Heure"));
    }
    widget.alwaysShowNotTraited->setChecked(m_critetia.alwaysShowNotTraited);
    widget.traited->setCheckState(m_critetia.traited);
    widget.createdDateTagAdv->setCurrentIndex(int(m_critetia.createdDateTagAdv));
    widget.createdDateTag->setCurrentIndex(int(m_critetia.createdDateTag));
    widget.modifiedDateTagAdv->setCurrentIndex(int(m_critetia.modifiedDateTagAdv));
    widget.modifiedDateTag->setCurrentIndex(int(m_critetia.modifiedDateTag));
    
    widget.createdDateTag->setVisible(m_critetia.createdDateTagAdv != BatchMergeCriteria::None);
    if(m_critetia.createdDateTagAdv == BatchMergeCriteria::None){
        widget.stackedCreatedDate->setVisible(false);
    }else if(m_critetia.createdDateTag >= BatchMergeCriteria::RelativeDate){
        widget.stackedCreatedDate->setVisible(true);
        widget.stackedCreatedDate->setCurrentWidget(widget.relativeCreatedDate);
        widget.relativeCreatedDate0->setVisible(m_critetia.createdDateTag == BatchMergeCriteria::RelativeDateTime);
        widget.relativeCreatedDate1->setVisible(m_critetia.createdDateTag == BatchMergeCriteria::RelativeDateTime);
    }else{
        widget.stackedCreatedDate->setVisible(true);
        widget.stackedCreatedDate->setCurrentWidget(widget.createdDate);
        widget.createdDate1->setVisible(m_critetia.createdDateTagAdv == BatchMergeCriteria::Since || m_critetia.createdDateTagAdv == BatchMergeCriteria::Beetween);
        widget.createdDate2->setVisible(m_critetia.createdDateTagAdv == BatchMergeCriteria::Before || m_critetia.createdDateTagAdv == BatchMergeCriteria::Beetween);
    }
    if(m_critetia.createdDateTag == BatchMergeCriteria::DateTime){
        widget.createdDate1->setMinimumSize(130, 0);
        widget.createdDate2->setMinimumSize(130, 0);
        widget.createdDate1->setDisplayFormat(tr("dd/MM/yyyy HH:mm:ss"));
        widget.createdDate2->setDisplayFormat(tr("dd/MM/yyyy HH:mm:ss"));
    }else if(m_critetia.createdDateTag == BatchMergeCriteria::Date){
        widget.createdDate1->setMinimumSize(0, 0);
        widget.createdDate2->setMinimumSize(0, 0);
        widget.createdDate1->setDisplayFormat(tr("dd/MM/yyyy"));
        widget.createdDate2->setDisplayFormat(tr("dd/MM/yyyy"));
    }else{
        widget.relativeCreatedDate0->setValue(m_critetia.relativeCreatedDate[0]);
        widget.relativeCreatedDate1->setValue(m_critetia.relativeCreatedDate[1]);
        widget.relativeCreatedDate2->setValue(m_critetia.relativeCreatedDate[2]);
        widget.relativeCreatedDate3->setValue(m_critetia.relativeCreatedDate[3]);
    }
    
    widget.modifiedDateTag->setVisible(m_critetia.modifiedDateTagAdv != BatchMergeCriteria::None);
    widget.relativeModifiedDate->setVisible(m_critetia.modifiedDateTagAdv != BatchMergeCriteria::None && m_critetia.modifiedDateTag >= BatchMergeCriteria::RelativeDate);
    if(m_critetia.modifiedDateTagAdv == BatchMergeCriteria::None){
        widget.stackedModifiedDate->setVisible(false);
    }else if(m_critetia.modifiedDateTag >= BatchMergeCriteria::RelativeDate){
        widget.stackedModifiedDate->setVisible(true);
        widget.stackedModifiedDate->setCurrentWidget(widget.relativeModifiedDate);
        widget.relativeModifiedDate0->setVisible(m_critetia.modifiedDateTag == BatchMergeCriteria::RelativeDateTime);
        widget.relativeModifiedDate1->setVisible(m_critetia.modifiedDateTag == BatchMergeCriteria::RelativeDateTime);
    }else{
        widget.stackedModifiedDate->setVisible(true);
        widget.stackedModifiedDate->setCurrentWidget(widget.modifiedDate);
        widget.modifiedDate1->setVisible(m_critetia.modifiedDateTagAdv == BatchMergeCriteria::Since || m_critetia.modifiedDateTagAdv == BatchMergeCriteria::Beetween);
        widget.modifiedDate2->setVisible(m_critetia.modifiedDateTagAdv == BatchMergeCriteria::Before || m_critetia.modifiedDateTagAdv == BatchMergeCriteria::Beetween);
    }
    if(m_critetia.modifiedDateTag == BatchMergeCriteria::DateTime){
        widget.modifiedDate1->setMinimumSize(130, 0);
        widget.modifiedDate2->setMinimumSize(130, 0);
        widget.modifiedDate1->setDisplayFormat(tr("dd/MM/yyyy HH:mm:ss"));
        widget.modifiedDate2->setDisplayFormat(tr("dd/MM/yyyy HH:mm:ss"));
    }else if(m_critetia.modifiedDateTag == BatchMergeCriteria::Date){
        widget.modifiedDate1->setMinimumSize(0, 0);
        widget.modifiedDate2->setMinimumSize(0, 0);
        widget.modifiedDate1->setDisplayFormat(tr("dd/MM/yyyy"));
        widget.modifiedDate2->setDisplayFormat(tr("dd/MM/yyyy"));
    }else{
        widget.relativeModifiedDate0->setValue(m_critetia.relativeModifiedDate[0]);
        widget.relativeModifiedDate1->setValue(m_critetia.relativeModifiedDate[1]);
        widget.relativeModifiedDate2->setValue(m_critetia.relativeModifiedDate[2]);
        widget.relativeModifiedDate3->setValue(m_critetia.relativeModifiedDate[3]);
    }
    widget.createdDate1->setDateTime(m_critetia.createdDate1);
    widget.createdDate2->setDateTime(m_critetia.createdDate2);
    widget.modifiedDate1->setDateTime(m_critetia.modifiedDate1);
    widget.modifiedDate2->setDateTime(m_critetia.modifiedDate2);
    
    setUpdatesEnabled(true);
}

void BatchFilterDlg::on_alwaysShowNotTraited_stateChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.alwaysShowNotTraited = (Qt::CheckState(v) == Qt::Checked);
}
void BatchFilterDlg::on_traited_stateChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.traited = Qt::CheckState(v);
}
void BatchFilterDlg::on_createdDateTagAdv_currentIndexChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.createdDateTagAdv = BatchMergeCriteria::TagDateAdv(v);
    updateDisplay();
}
void BatchFilterDlg::on_createdDateTag_currentIndexChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.createdDateTag = BatchMergeCriteria::TagDate(v);
    updateDisplay();
}
void BatchFilterDlg::on_modifiedDateTagAdv_currentIndexChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.modifiedDateTagAdv = BatchMergeCriteria::TagDateAdv(v);
    updateDisplay();
}
void BatchFilterDlg::on_modifiedDateTag_currentIndexChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.modifiedDateTag = BatchMergeCriteria::TagDate(v);
    updateDisplay();
}
void BatchFilterDlg::on_createdDate1_dateTimeChanged(const QDateTime& v){
    if(!updatesEnabled())
        return;
    m_critetia.createdDate1 = v;
}
void BatchFilterDlg::on_createdDate2_dateTimeChanged(const QDateTime& v){
    if(!updatesEnabled())
        return;
    m_critetia.createdDate2 = v;
}
void BatchFilterDlg::on_modifiedDate1_dateTimeChanged(const QDateTime& v){
    if(!updatesEnabled())
        return;
    m_critetia.modifiedDate1 = v;
}
void BatchFilterDlg::on_modifiedDate2_dateTimeChanged(const QDateTime& v){
    if(!updatesEnabled())
        return;
    m_critetia.modifiedDate2 = v;
}
void BatchFilterDlg::on_name_textChanged(const QString& v){
    if(!updatesEnabled())
        return;
    m_critetia.name = v;
}
void BatchFilterDlg::on_relativeCreatedDate0_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeCreatedDate[0] = v;
}
void BatchFilterDlg::on_relativeCreatedDate1_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeCreatedDate[1] = v;
}
void BatchFilterDlg::on_relativeCreatedDate2_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeCreatedDate[2] = v;
}
void BatchFilterDlg::on_relativeCreatedDate3_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeCreatedDate[3] = v;
}
void BatchFilterDlg::on_relativeModifiedDate0_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeModifiedDate[0] = v;
}
void BatchFilterDlg::on_relativeModifiedDate1_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeModifiedDate[1] = v;
}
void BatchFilterDlg::on_relativeModifiedDate2_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeModifiedDate[2] = v;
}
void BatchFilterDlg::on_relativeModifiedDate3_valueChanged(const int& v){
    if(!updatesEnabled())
        return;
    m_critetia.relativeModifiedDate[3] = v;
}
